package com.wenan.ui.controls.dateChooser
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.DateChooser;
	import mx.controls.Image;
	import mx.core.IUITextField;
	import mx.core.mx_internal;
	import mx.events.FlexEvent;
	
	[Style(name="bgurl", inherit="no", type="String")]
	
	public class DateChooserExt extends DateChooser
	{
		private var cellHeight:Number = 19.571428571428573;
		private var cellWidth:Number = 23.571428571428573;
		
		public function DateChooserExt()
		{
			super();
			this.setStyle("selectionIndicatorSkin", DateChooserSelectionIndicatorSkinEx);
		}
		
		private var bg:Image = null;
		override protected function createChildren():void
		{
			
			this.bg = new Image();
			var url:String = this.getStyle("bgurl");
			this.bg.source = url;
			this.addChild(this.bg);
			super.createChildren();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			var url:String = this.getStyle("bgurl");
			this.bg.source = url;
			
			this.bg.width = unscaledWidth;
			this.bg.height = unscaledHeight;
			this.mx_internal::border.visible = false;
			this.mx_internal::background.visible = false;
			this.mx_internal::dateGrid.y -= 10;
			
			if (this.mx_internal::downYearButton)
			{
				this.mx_internal::downYearButton.x = 30;
				this.mx_internal::downYearButton.y -= 5;
				this.mx_internal::downYearButton.hitArea = null;
				this.mx_internal::downYearButton.buttonMode = true;
				this.mx_internal::downYearButton.useHandCursor = true;
			}
			
			if (this.mx_internal::upYearButton)
			{
				this.mx_internal::upYearButton.x = 140;
				this.mx_internal::upYearButton.y += 4;
				this.mx_internal::upYearButton.hitArea = null;
				this.mx_internal::upYearButton.buttonMode = true;
				this.mx_internal::upYearButton.useHandCursor = true;
			}
			
			if (this.mx_internal::backMonthButton)
			{
				this.mx_internal::backMonthButton.buttonMode = true;
				this.mx_internal::backMonthButton.useHandCursor = true;
			}
			if (this.mx_internal::fwdMonthButton)
			{
				this.mx_internal::fwdMonthButton.buttonMode = true;
				this.mx_internal::fwdMonthButton.useHandCursor = true;
			}
			if (this.mx_internal::monthDisplay)
			{
				this.mx_internal::monthDisplay.x = 65;
			}
			
			if (this.mx_internal::yearDisplay)
			{
				this.mx_internal::yearDisplay.x = 85;
			}
		
			this.mx_internal::dateGrid.setStyle("paddingLeft", 0);
			this.mx_internal::dateGrid.setStyle("paddingRIght", 0);
			this.mx_internal::dateGrid.setStyle("horizontalGap", 0);
			this.mx_internal::dateGrid.setStyle("horizontalGap", 0);
			this.mx_internal::dateGrid.addEventListener(MouseEvent.MOUSE_MOVE, dateGridMoveHandler);
		}
		
		private function dateGridMoveHandler(event:MouseEvent):void
		{
			var paddingLeft:Number = this.mx_internal::dateGrid.getStyle("paddingLeft");
			var paddingTop:Number = this.mx_internal::dateGrid.getStyle("paddingTop");
			var firstColX:Number = this.mx_internal::dateGrid.mx_internal::dayBlocksArray[0][0].x;
			var lastColX:Number = this.mx_internal::dateGrid.mx_internal::dayBlocksArray[6][0].x;
			var firstRowY:Number = this.mx_internal::dateGrid.mx_internal::dayBlocksArray[6][0].y + cellHeight;
			
			var mousePoint:Point = new Point(event.stageX, event.stageY);
			mousePoint = this.mx_internal::dateGrid.globalToLocal(mousePoint);
			var mouseY:Number = mousePoint.y;
			var mouseX:Number = mousePoint.x;
			
			if (mouseX < firstColX ||
				mouseX > lastColX + cellWidth ||
				mouseY < firstRowY)
				return;
			
			var rowIndex:int = Math.floor((mouseY-paddingTop) / cellHeight);
			var colIndex:int = Math.floor((mouseX-paddingLeft) / cellWidth);
			
			colIndex = Math.min(colIndex, 6);
			rowIndex = Math.min(rowIndex, 6);
			
			var selCell:IUITextField = this.mx_internal::dateGrid.mx_internal::dayBlocksArray[colIndex][rowIndex];
			
			// If it is disabled, we're done.
			if (rowIndex == 0)
				return;
			
			if (mouseY >= selCell.y &&
				mouseY <= selCell.y + cellHeight &&
				mouseX >= selCell.x &&
				mouseX <= selCell.x + cellWidth)
			{
				var newDate:Date = new Date(displayedYear, displayedMonth, int(selCell.text));
				this.dispatchEvent(new CalenDarLayoutEvent(CalenDarLayoutEvent.CALENDARLAYOUTMOVE, newDate));
			}
		}
	}
}